//
//  HCPopoverViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/5/31.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "HCPopoverViewController.h"

@interface HCPopoverViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HCPopoverViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
    if (_topTitle && ![_topTitle isEqualToString:@""])
    {
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text =  self.topTitle;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:16.0f];
        [self.view addSubview:label];
        
        [label mas_remakeConstraints:^(MASConstraintMaker *make)
        {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(self.tableView.mas_top);
            make.height.mas_equalTo(44);
        }];
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make)
         {
             make.left.mas_equalTo(0);
             make.right.mas_equalTo(0);
             make.top.mas_equalTo(label.mas_bottom);
             make.bottom.mas_equalTo(0);
         }];
    }
}

- (void)viewWillLayoutSubviews
{
    self.tableView.frame = self.view.bounds;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.selectCallback != nil)
    {
        self.selectCallback(indexPath);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* curCell = [tableView dequeueReusableCellWithIdentifier:@"HCPopoverCell"];
    
    if (!curCell)
    {
        curCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"HCPopoverCell"];
    }
    
    curCell.textLabel.text = self.titles[indexPath.row];
    
    return curCell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
